package com.jmlee.controller;

import com.jmlee.exception.Asserts;
import com.jmlee.pojo.CommonPage;
import com.jmlee.pojo.CommonResult;
import com.jmlee.pojo.Items;
import com.jmlee.service.ItemsService;
import com.jmlee.vo.ItemDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 商品服务接口
 * @Author jmLee
 * @Date 2021/3/8 22:59
 * @Version 1.0
 */
@RestController
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    /**
     * 获取商品列表
     *
     * @param curPage
     * @param pageSize
     * @param sort
     * @param cid
     * @return
     */
    @GetMapping("/items")
    public CommonResult itemList(@RequestParam("curPage") Integer curPage,
                                 @RequestParam("pageSize") Integer pageSize,
                                 @RequestParam("sort") Integer sort,
                                 @RequestParam("cid") Long cid) {

        if (sort == null) {
            Asserts.fail("排序参数不能为空");
        }

        CommonPage<Items> itemsList = itemsService.getItemsList(cid, sort, curPage, pageSize);

        return CommonResult.success(itemsList);
    }


    @GetMapping("/search")
    public CommonResult search(String keyword, Integer curPage, Integer pageSize) {

        CommonPage<Items> searchItems = itemsService.searchItem(keyword,curPage,pageSize);

        return CommonResult.success(searchItems);
    }

    @GetMapping("/item/detail/{itemId}")
    public CommonResult itemDetail(@PathVariable("itemId") Long itemId) {

        if (itemId == null) {
            Asserts.fail("商品Id不能为空");
        }

        ItemDetailVO itemDetail = itemsService.getItemDetail(itemId);

        return CommonResult.success(itemDetail);
    }


    @GetMapping("/items/special")
    public CommonResult specialItems(Integer itemType){

        if(itemType == null) {
            Asserts.fail("请求参数不能为空");
        }
        CommonPage<Items> specialItems = itemsService.getSpecialItems(itemType);

        return CommonResult.success(specialItems);
    }


}
